package uranium.gui.add;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import net.sf.jasperreports.engine.JRException;
import uranium.combo.MyCombo;
import uranium.combo.MyComboFatt;
import uranium.db.DBManager;
import uranium.gui.HomeGUI;
import uranium.utility.ControlloDati;

import java.awt.Rectangle;


public class AddNewTelGUI extends JDialog{

	//private HomeGUI jFrame = null;
	private JFrame jFrame = null;//  @jve:decl-index=0:visual-constraint="251,125"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JTextField jTextField = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField1 = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField2 = null;
	private JLabel jLabel3 = null;
	private JTextField jTextField3 = null;
	private JLabel jLabel4 = null;
	private JTextField jTextField4 = null;
	private JLabel jLabel5 = null;
	private MyCombo jComboBox = null;
	private JButton jButton = null;
	private JLabel jLabel6 = null;
	private MyComboFatt jComboBox1 = null;
	private JButton jButton1 = null;
	private DBManager dbm = null;
	private JButton jButton2 = null;
	private JButton jButton3 = null;
	public AddNewTelGUI(DBManager dbm, JFrame jFrame){
		super(jFrame, true);
		this.jFrame = jFrame;
		this.dbm = dbm;
		initialize();
	}
	
	private void initialize(){
		//jFrame = new JFrame();
		this.setSize(new Dimension(440, 300));
		this.setTitle("Inserisci Telefono Nuovo");
		this.setPreferredSize(new Dimension(250, 188));
		this.setContentPane(getJContentPane());
		centro();
		this.setVisible(true);
		
	}
	
	private void centro(){
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}
	
	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel6 = new JLabel();
			jLabel6.setText("Fattura");
			jLabel6.setBounds(new Rectangle(15, 180, 50, 16));
			jLabel5 = new JLabel();
			jLabel5.setText("Fornitore");
			jLabel5.setBounds(new Rectangle(15, 156, 61, 16));
			jLabel4 = new JLabel();
			jLabel4.setText("Prezzo OUT");
			jLabel4.setBounds(new Rectangle(15, 132, 76, 16));
			jLabel3 = new JLabel();
			jLabel3.setText("Prezzo IN");
			jLabel3.setBounds(new Rectangle(15, 110, 63, 16));
			jLabel2 = new JLabel();
			jLabel2.setText("Modello");
			jLabel2.setBounds(new Rectangle(15, 88, 54, 16));
			jLabel1 = new JLabel();
			jLabel1.setText("IMEI ");
			jLabel1.setBounds(new Rectangle(15, 66, 36, 16));
			jLabel = new JLabel();
			jLabel.setText(" Codice a Barre ");
			jLabel.setBounds(new Rectangle(15, 44, 100, 16));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setPreferredSize(new Dimension(250, 166));
			jPanel.add(getJTextField(), null);
			jPanel.add(jLabel, null);
			jPanel.add(jLabel1, null);
			jPanel.add(getJTextField1(), null);
			jPanel.add(jLabel2, null);
			jPanel.add(getJTextField2(), null);
			jPanel.add(jLabel3, null);
			jPanel.add(getJTextField3(), null);
			jPanel.add(jLabel4, null);
			jPanel.add(getJTextField4(), null);
			jPanel.add(jLabel5, null);
			jPanel.add(getJComboBox(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(jLabel6, null);
			jPanel.add(getJComboBox1(), null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJButton2(), null);
			jPanel.add(getJButton3(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setPreferredSize(new Dimension(200, 22));
			jTextField.setBounds(new Rectangle(130, 41, 200, 22));
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setPreferredSize(new Dimension(200, 22));
			jTextField1.setBounds(new Rectangle(130, 63, 200, 22));
		}
		return jTextField1;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setPreferredSize(new Dimension(200, 22));
			jTextField2.setBounds(new Rectangle(130, 85, 200, 22));
		}
		return jTextField2;
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
			jTextField3.setPreferredSize(new Dimension(200, 22));
			jTextField3.setBounds(new Rectangle(130, 107, 200, 22));
		}
		return jTextField3;
	}

	/**
	 * This method initializes jTextField4	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField4() {
		if (jTextField4 == null) {
			jTextField4 = new JTextField();
			jTextField4.setPreferredSize(new Dimension(200, 22));
			jTextField4.setBounds(new Rectangle(130, 129, 200, 22));
		}
		return jTextField4;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new MyCombo(dbm);
			jComboBox.setPreferredSize(new Dimension(200, 20));
			jComboBox.setBounds(new Rectangle(130, 151, 200, 22));
			jComboBox.stateChange();
		}
		return jComboBox;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Nuovo");
			jButton.setBounds(new Rectangle(350, 148, 69, 26));
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						apriAddNewFornGUI();
						jComboBox.stateChange();
					}
					catch(Exception e14){
						System.err.println(e14);
					}
				}
			});
		}
		return jButton;
	}
	
	private void apriAddNewFornGUI(){
		//final AddNewCancGUI padre = this;
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				AddNewFornGUI cerca = new AddNewFornGUI(dbm, jFrame);
				jComboBox.stateChange();
			}
		});

	}

	/**
	 * This method initializes jComboBox1	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox1() {
		if (jComboBox1 == null) {
			jComboBox1 = new MyComboFatt(dbm);
			jComboBox1.setPreferredSize(new Dimension(200, 22));
			jComboBox1.setBounds(new Rectangle(130, 174, 200, 22));
			jComboBox1.stateChange();
		}
		return jComboBox1;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Inserisci");
			jButton1.setBounds(new Rectangle(260, 220, 83, 26));
			jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						inserisci();
					}
					catch(Exception e27){
						System.err.println(e27);
					}
				}
			});
		}
		return jButton1;
	}
	private synchronized void inserisci() throws JRException{
		boolean error = false;
		/*if(jTextField.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Codice a Barre non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}*/
		if(jTextField1.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Imei non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField2.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Modello non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		/*if(jTextField3.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoIN non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}/*
		else if(!uranium.utility.ControlloDati.prezzoCorretto(jTextField3.getText().trim())){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoIN mal formato (0.00)", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}*/
		/*if(jTextField4.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoOUT DA non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}/*
		else if(!uranium.utility.ControlloDati.prezzoCorretto(jTextField4.getText().trim())){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoOUT DA mal formato (0.00)", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}*/
		/*if(jTextFieldPA.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoOUT A non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}/*
		else if(!uranium.utility.ControlloDati.prezzoCorretto(jTextFieldPA.getText().trim())){
			JOptionPane.showMessageDialog(jFrame, "Campo PrezzoOUT A mal formato (0.00)", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		/*
		if(jComboBox.getSelectedItem() == null){
			JOptionPane.showMessageDialog(jFrame, "Campo fornitore non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jComboBox1.getSelectedItem() == null){
			JOptionPane.showMessageDialog(jFrame, "Campo fattura non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		*/
		if(error == false){
			String cbarre = jTextField.getText();
			String imei = jTextField1.getText();
			String modello = jTextField2.getText();
			double prezzoin = Double.parseDouble(ControlloDati.aggiustaPrezzo(jTextField3.getText()));
			double prezzout = Double.parseDouble(ControlloDati.aggiustaPrezzo(jTextField4.getText()));
			//String prezzouta = ControlloDati.aggiustaPrezzo(jTextFieldPA.getText());
			String fornitore = (String)jComboBox.getSelectedItem();
			String fattura = (String)jComboBox1.getSelectedItem();
			String insert="INSERT INTO telefonin (cbarre,imei,modello,prezzoin,prezzoout,fornitore,fattura) VALUES('"+cbarre+"','"+imei+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+fornitore+"','"+fattura+"')";

			dbm.executeQuery(insert);
			this.dispose();
		}
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(85, 220, 83, 26));
			jButton2.setText("Annulla");
			jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e32){
						e32.printStackTrace();
					}
				}
			});
		}
		return jButton2;
	}
	
	private void close(){
		this.dispose();
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setBounds(new Rectangle(350, 174, 69, 26));
			jButton3.setText("Nuova");
			jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						apriAddNewFattGUI();
						jComboBox1.stateChange();
					}
					catch(Exception err4){
						err4.printStackTrace();
					}
				}
			});
		}
		return jButton3;
	}

	private void apriAddNewFattGUI(){
		//final AddNewCancGUI padre = this;
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				AddNewFattGUI cerca = new AddNewFattGUI(dbm, jFrame);
				jComboBox1.stateChange();
			}
		});

	}
}
